package com.front.web.lvyouXcxWeb;

import com.bootdo.common.utils.GPSUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.lvyou.domain.LvyouScenicMarker;
import com.bootdo.maputils.GpsJisuanUitils;
import com.bootdo.wx.domain.Config;
import com.front.web.wx.utils.WechatUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.geom.Point2D;
import java.util.*;

@RequestMapping("/f/web/page")
@Controller
public class WebPageController extends LvyouSupperController {

    /**
     * http://127.0.0.1//f/web/page/map/index
     * http://yvnwb3.natappfree.cc/f/web/page/map/index
     *
     * @param path
     * @return
     */
    @RequestMapping("/map/{path}")
    public String index(@PathVariable String path){
        System.err.println(path);
        return "map/"+path;
    }

    /**
     *  http://127.0.0.1/f/web/page/getJsApi
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/getJsApi")
    @ResponseBody
    public R getJsApi(
            @RequestParam("url") String url,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        Config config  = configService.getByUserId(1l);
        Map<String, String> result = new HashMap<String, String>();
        result = WechatUtil.getJsApi(config,url);
      /*  request.setAttribute("signature", result.get("signature"));
        request.setAttribute("nonceStr", result.get("nonceStr"));
        request.setAttribute("timestamp", result.get("timestamp"));
        request.setAttribute("weChatAppId",config.getWxAppId());*/
        result.put("weChatAppId", config.getWxAppId());
        return R.ok().put("jsApi",result);
    }

    /**
     *
     * 电子围栏判断  判断某个gps位置是否在某个区域内
     * http://127.0.0.1/f/web/page/checkIsDiv?lon=113.94151070233218&lat=36.9094713779987
     * @param lon
     * @param lat
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/checkIsDiv")
    @ResponseBody
    public R checkIsDiv(
            @RequestParam("lon")Double lon,
            @RequestParam("lat")Double lat,
            @RequestParam("spotId")Long spotId,
            HttpServletRequest request, HttpServletResponse response){
        List<LvyouScenicMarker> list =  lvyouScenicMarkerService
                .list(MapBuild.getInstand().setKV("type",999)
                        .setKV("refId", spotId)
                );
        if(StringUtils.isEmpty(request.getParameter("b"))){
            double gps [] =GPSUtil.gcj02_To_Bd09(lat, lon);
            lat = gps[0];
            lon = gps[1];
        }
        Point2D.Double my = new Point2D.Double(lon,lat);
        List<Point2D.Double> pts = new ArrayList<>();
        list.forEach(item ->{
            Point2D.Double p2d = new Point2D.Double();
            p2d.setLocation(item.getLon(),item.getLat());
            pts.add(p2d);
        });
        boolean b =  GpsJisuanUitils.isInPolygon(my,pts);
        if(b){
            return R.ok();
        }
        return R.error();
    }

}
